package com.lw.ucenter.controller;

import com.lw.commonutil.result.Result;
import com.lw.commonutil.result.ResultCodeEnum;
import com.lw.commonutil.utils.JwtUtils;
import com.lw.commonutil.vo.UcenterMemberVo;
import com.lw.servicebase.exception.GuliException;
import com.lw.ucenter.entity.UcenterMember;
import com.lw.ucenter.entity.vo.RegisterVo;
import com.lw.ucenter.service.UcenterMemberService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

@RestController
@RequestMapping("/api/ucenter/member")
public class UcenterMemberController {

    @Autowired
    private UcenterMemberService ucenterMemberService;

    //根据手机号和密码登录
    @PostMapping("/login")
    public Result login(@RequestBody UcenterMember ucenterMember){
        //调用service方法进行登录
        String token = ucenterMemberService.login(ucenterMember);
        if (token!=null){
            return Result.ok(token);
        }else {
            return Result.fail();
        }
    }
    @PostMapping("/register")
    public Result register(@RequestBody RegisterVo registerVo){
        ucenterMemberService.register(registerVo);
        return Result.ok();
    }

    //根据token获取用户信息
    @GetMapping("/auth/profile")
    public Result profile(HttpServletRequest request){
        //根据token获取用户id
        String id = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMember ucenterMember = ucenterMemberService.getById( id );
        return Result.ok( ucenterMember );
    }
    //rpc根据用户id获取用户信息
    @PostMapping("/inner/userInfo/{id}")
    public UcenterMemberVo profile(@PathVariable("id") String id){
        UcenterMember ucenterMember = ucenterMemberService.getById( id );
        UcenterMemberVo memberVo = new UcenterMemberVo();
        BeanUtils.copyProperties( ucenterMember,memberVo );
        return memberVo;
    }
    //rpc根据日期查询当天注册人数
    @GetMapping("/inner/countRegister/{day}")
    public Integer countRegister(@PathVariable String day){
        Integer count = ucenterMemberService.countRegister(day);
        return count;
    }
}
